package com.ctsi.security.server;

import com.ctsi.framework.common.startrun.ComponentFactory;
import com.ctsi.security.I_PermisionTypeExtend;
import com.ctsi.security.PermissionTypeSer;
import com.ctsi.security.domain.Permission;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class PermisionTypeExtendManage {
	private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(PermisionTypeExtendManage.class);
	private volatile static PermisionTypeExtendManage instance;
	Map<String, I_PermisionTypeExtend> ptemap = new HashMap<>();
	PermissionTypeSer permissionTypeService;

	public static PermisionTypeExtendManage getInstance() {
		if (instance != null) {
			return instance;
		}
		PermisionTypeExtendManage temp = new PermisionTypeExtendManage();
		List<I_PermisionTypeExtend> ptelist = ComponentFactory.getBeanForType(I_PermisionTypeExtend.class);
		for (I_PermisionTypeExtend permisionTypeExtend : ptelist) {
			temp.ptemap.put(permisionTypeExtend.getRoot(), permisionTypeExtend);
		}
		temp.permissionTypeService = (PermissionTypeSer) ComponentFactory.getBean("permissionTypeService");
		instance = temp;
		return temp;
	}

	public I_PermisionTypeExtend getPermisionTypeExtendByParent(String parent) {
		return ptemap.get(parent);
	}

	public I_PermisionTypeExtend getPermisionTypeExtendById(String id) {
		for (String key : ptemap.keySet()) {
			I_PermisionTypeExtend permisionTypeExtend = ptemap.get(key);
			if (permisionTypeExtend.getId().equals(id)) {
				return permisionTypeExtend;
			}
		}
		return null;
	}

	public List<Permission> getPermision(String parent, String loginId, List<String> beans, List<String> path) {
		logger.info("path {}", path);
		List<Permission> list = permissionTypeService.findAllChildPermision(parent, loginId);
		for (String id : beans) {
			I_PermisionTypeExtend permisionTypeExtend = getPermisionTypeExtendById(id);
			list = permisionTypeExtend.getExtends(path, list);
		}
		return list;
	}

	public void setPermissionTypeService(PermissionTypeSer permissionTypeService) {
		this.permissionTypeService = permissionTypeService;
	}
	

}
